/*
 * AppointmentPanel.java
 */
package edu.gatech.oad.schedule;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import edu.gatech.oad.ui.UserPanel;
import edu.gatech.oad.user.Patient;


public class AppointmentViewPanel extends JPanel{
	private static final long serialVersionUID = 1L;
	private GridBagConstraints gbc = new GridBagConstraints();
	private Patient patient;
	private List<Appointment> appt;
	private JList appointmentViewList;
	private JPanel buttonPanel, listPanel, appointmentInfoPanel;
	private UserPanel userPanel;
	private DefaultListModel viewModel = new DefaultListModel();
	private final String[] buttonTitles = {"Schedule Appointment", "Edit Appointment", "Delete Appointment"};
	private final String dateBegin = "Appointment: ", reasonsBegin = "For the following reasons: ", doctorBegin = "Doctor: ";
	private String date = "", reasons = "", doctor = "";
	private JLabel dateLabel, reasonsLabel, doctorLabel;
	
	public AppointmentViewPanel(Patient patient, UserPanel userPanel){
		this.patient = patient;
		this.userPanel = userPanel;
		this.setLayout(new GridBagLayout());
		this.setBackground(Color.white);
		gbc.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc.fill = GridBagConstraints.BOTH;

		addToListPanel();
		addToAppointmentInfoPanel();
		addToButtonPanel();

	}
	
	private void addToAppointmentInfoPanel(){
		this.appointmentInfoPanel = new JPanel();
		appointmentInfoPanel.setPreferredSize(new Dimension(300,300));

		appointmentInfoPanel.setBorder(new TitledBorder("Appointment Informations"));
		appointmentInfoPanel.setLayout(new BoxLayout(appointmentInfoPanel, BoxLayout.Y_AXIS));
		this.dateLabel = new JLabel(dateBegin + date);
		this.reasonsLabel = new JLabel(reasonsBegin + reasons);
		this.doctorLabel = new JLabel(doctorBegin + doctor);
		appointmentInfoPanel.add(dateLabel);
		gbc.gridheight = 1;
		appointmentInfoPanel.add(reasonsLabel);
		appointmentInfoPanel.add(doctorLabel);
		gbc.gridy = 0;
		gbc.gridx = 1;
		add(appointmentInfoPanel, gbc);
	}
	
	private void addToButtonPanel(){
		this.buttonPanel = new JPanel();
		buttonPanel.setPreferredSize(new Dimension(300,300));
		buttonPanel.setBorder(new TitledBorder("Appointment Actions"));
		buttonPanel.setLayout(new GridLayout(3,1));
		for(String titles : buttonTitles){
			JButton schedButtons = new JButton(titles);
			schedButtons.addActionListener(new ScheduleButtonListener());
			schedButtons.setActionCommand(titles);
			if(patient.getAppointments().isEmpty() && schedButtons.getActionCommand().equals("Schedule Appointment")){
				schedButtons.setEnabled(false);
			}
			buttonPanel.add(schedButtons);
		}
		gbc.gridy = 1;
		gbc.gridx = 1;
		gbc.weightx = 1; 
		gbc.weighty = 1;
		add(buttonPanel, gbc);
	}
	
	private class ScheduleButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(e.getActionCommand().equals("Schedule Appointment")){
				userPanel.getTabbedPane().removeTabAt(userPanel.getTabbedPane().getSelectedIndex());
				userPanel.addNewTab("Schedule Appointment", new AppointmentPanel(patient, userPanel));
			}else if(e.getActionCommand().equals("Edit Appointment")){
				//TODO Add Edit Appointment in the Panel
			}else if(e.getActionCommand().equals("Delete Appointment")){
				AppointmentController apptCtrlr = new AppointmentController(patient);
				apptCtrlr.deleteAppointment(appt.get(appointmentViewList.getSelectedIndex()));
				viewModel.removeElement(appointmentViewList.getSelectedIndex());
				//appointmentViewList.
			}
		}
	}
	
	private void addToListPanel(){
		this.listPanel = new JPanel();
		listPanel.setPreferredSize(new Dimension(300,600));
		listPanel.setBorder(new TitledBorder("Future Appointments"));
		listPanel.setLayout(new BorderLayout());
		appt = new ArrayList<Appointment>();
		for(Appointment appts : patient.getAppointments()){
			appt.add(appts); 
		}
		if(appt != null){ 
			appointmentViewList = new JList(viewModel);
			for(Appointment appointment : appt){
				viewModel.addElement(appointment);
			}
		}else{
			listPanel.add(new JLabel("You have no future appointment, schedule one?"), BorderLayout.NORTH);
		}
		appointmentViewList.setPreferredSize(new Dimension(100,100));
		appointmentViewList.addListSelectionListener(new AppointmentListListener());
		listPanel.add(appointmentViewList, BorderLayout.CENTER);
		gbc.gridheight = 2;
		gbc.gridx = 0;
		gbc.gridy = 0;
		add(listPanel, gbc);
	}
	
	private class AppointmentListListener implements ListSelectionListener{
		public void valueChanged(ListSelectionEvent e) {
			date = appt.get(appointmentViewList.getSelectedIndex()).getDate().toString();
			if(!appt.get(appointmentViewList.getSelectedIndex()).getReason().isEmpty())
				reasons = appt.get(appointmentViewList.getSelectedIndex()).getReason().toString();
			else
				reasons = "None Provided";
			if(reasons.isEmpty()) reasons = "None Provided";
			doctor = appt.get(appointmentViewList.getSelectedIndex()).getDoctor().getName();
			dateLabel.setText(dateBegin + date);
			reasonsLabel.setText(reasonsBegin + reasons);
			doctorLabel.setText(doctorBegin + doctor);
		}
	}
}